# Use a lightweight JRE (not JDK) for production
FROM eclipse-temurin:21-jre-jammy

# Set the Java options for JVM
ENV JAVA_OPTS="-server -Xmx1024M"

# Sets the working directory inside the container
WORKDIR /app

# Set JVM options (best practices for containers)
ENV JAVA_OPTS="-server -XX:MaxRAMPercentage=75.0 -XX:+UseContainerSupport -XX:+ExitOnOutOfMemoryError"

# Exposes the port the app runs on
EXPOSE 8080

# Set the default active profile as 'prod'
ENV SPRING_PROFILES_ACTIVE=prod

# Run as non-root user (security best practice)
RUN adduser --system --group appuser && chown -R appuser:appuser /app
USER appuser

# Runs the application
ENTRYPOINT ["java", "-jar", "family-expense-api.jar"]

# Builds the image.
# The defalut tag is latest. You can define any you need. Example: family-expense-api:1.0
# The -f specifies the Dockerfile path. With this argument we can run build command out of docker file directory. This makes the parent directory of docker file directory the execution context.
# docker build --tag family-expense-eclipse-temurin:21-jre-jammy -f .\docker\family-expense-eclipse-temurin\Dockerfile .

# Runs the image.
# The defalut tag is latest. You can define any you need. Example: family-expense-api:1.0
# The --restart always defines the restart policy. Always restart the container if it stops. If it is manually stopped, it is restarted only when Docker daemon restarts or the container itself is manually restarted.
# The -p 8080:8080 defines the port mapping between host and container.
# The -v /data/family-expense/job/logs:/usr/local/tomcat/logs defines mapping directory between host and container.

# Runs container on local still with local profile.
# docker run -d --restart always -p 8090:8080 -e SPRING_PROFILES_ACTIVE=local --name=family-expense-eclipse-temurin21-slim -v D:/Temp/family-expense-app:/app family-expense-eclipse-temurin:21-jre-jammy

# Runs container on server with default prod profile.
# docker run -d --restart always -p 8090:8080 --name=family-expense-eclipse-temurin21-slim -v /data/family-expense/app:/app family-expense-eclipse-temurin:21-jre-jammy

# Runs terminal in the container.
# docker exec -it [container-id-can-get-from-last-step] bash

# Saves the image to a local file.
# The defalut tag is latest. You can define any you need. Example: family-expense-api:1.0
# docker save -o d:\family-expense-eclipse-temurin21-slim.tar family-expense-eclipse-temurin:21-jre-jammy

# Loads the local image file
# docker load -i d:\family-expense-eclipse-temurin21-slim.tar